package crew.gwt.client.widgets.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ScrollPanel;

import crew.gwt.client.domain.PersistableEntity;
import crew.gwt.client.widgets.ClassicLabel;
import crew.gwt.client.widgets.handler.OnActionEntityHandler;

public abstract class EntityGrid<T extends PersistableEntity> extends Composite implements ClickHandler {

        private List<T> entities = new ArrayList<T>();
        private FlexTable table = new FlexTable();
        private ScrollPanel scrollPanel = new ScrollPanel(table);
        private OnActionEntityHandler<T> onSelectHandler = null;
        
        public EntityGrid(final T wildcartType) {
                initWidget(scrollPanel);
//                scrollPanel.setWidth("1000px");
//              table.getColumnFormatter().setWidth(0, "50px");
                table.setBorderWidth(2);
                table.setCellPadding(5);
        		table.addStyleName("EntityGrid");
                setHeaders(wildcartType);
        }
        
        protected void setHeaders(T t) {
            int i = 1;
            for(String header : t.getHeadersValues()){
                    table.setWidget(0, i, new ClassicLabel(header));
                    i++;
            }
    		table.getRowFormatter().addStyleName(0, "EntityGrid-RowHeader");
        }

        public void addEntity(T t){
                if(!entities.contains(t)){
                        entities.add(t);
                        setEntityRow(t,entities.size());
                }
        }
        
        public void addEntities(Collection<T> list){
                for(T t : list)
                        addEntity(t);
        }
        
        public void clear() {
                entities.clear();
                while(table.getRowCount()!=1)
                        table.removeRow(1);
        }
        
        public boolean editEntity(T t){
                int index = entities.indexOf(t);
                if(index > -1){
                        entities.remove(index);
                        entities.add(index, t);
                        setEntityRow(t,index+1);
                        return true;
                }
                return false;
        }
        
        public boolean removeEntity(T t){
                int index = entities.indexOf(t);
                if(index > -1){
                        entities.remove(index);
                        table.removeRow(index+1);
                        return true;
                }
                return false;
        }
        
        public List<T> getEntities(){
                return entities;
        }
        
        @Override
        public void onClick(ClickEvent e) {
                if(onSelectHandler != null){
                        Button b = (Button) e.getSource();
                        int indexOfEntity = b.getTabIndex()-1;
                        if( indexOfEntity >= 0 && indexOfEntity < entities.size() )
                                onSelectHandler.onSelectEntity(entities.get(indexOfEntity));
                }
        };
        
        protected void setEntityRow(T t,final int index){
                Button b = new Button("Select",this);
                b.setTabIndex(index);
                table.setWidget(index, 0, b);
                
        		if( ( index%2 ) == 0)
        			table.getRowFormatter().addStyleName(index, "EntityGrid-RowClassic");
        		else
        			table.getRowFormatter().addStyleName(index, "EntityGrid-RowAlt");
                
                int i = 1;
                for(String value : t.getValues()){
                        table.setWidget(index, i++, new ClassicLabel(value));
                }
        }

        public void setOnSelectHandler(OnActionEntityHandler<T> onSelectHandler) {
                this.onSelectHandler = onSelectHandler;
        }
        
        
        protected FlexTable getTable(){
        	return table;
        }
}